<?php

/**
 * Provides a customizable captcha images and input field
 *
 * TODO: Add customization support (change image size, font, background etc)
 * TODO: Ajax reload image link
 * TODO: Captcha play() Audio support
 *
 * Uses open source 3rdparty captcha library: http://www.phpcaptcha.org
 */
class tx_zlib_viewhelper_captcha extends tx_zlib_abstract_viewhelper
{

	/**
	 * Instance of Securimage class
	 * @var <object> Securimage
	 */
	private $objSecurimage = null;


	/**
	 * Entry point
	 */
	public function captcha() {
		require_once(t3lib_extMgm::extPath('zlib').'3rdparty/securimage/securimage.php');
		if(!$this->objSecurimage instanceof Securimage) {
			$this->objSecurimage = new Securimage();

			// Construct url to the securimage.php file
			$this->securimageUrl		= t3lib_div::getIndpEnv('TYPO3_SITE_URL')
											. t3lib_extMgm::siteRelPath('zlib')
											. '3rdparty/securimage/';

			// Assign values to view
			$this->view->securimageUrl	= $this->securimageUrl;
			//echo "Securimage Url: " . $this->securimageUrl;
		}

		return $this;
	}

	/**
	 *
	 * @return <bool>
	 */
	public function isValid() {
		if($this->objSecurimage->check(t3lib_div::_POST('captcha_code')) == false) {
			return false;
		}
		else {
			return true;
		}
	}

	/**
	 * Quick render captcha based on template
	 */
	public function render($viewName = null) {
		return $this->getView()->render('captcha');
	}

	/**
	 * Renders a captcha image.
     * @param string $name
     * @param array $attribs
	 * @return string
    */
	public function captchaImage($name, $attribs = array())
	{
		// TODO

		return $img;
	}

	/**
	 * Renders an input field for a captcha image
     * @param string $name
     * @param array $attribs
	 * @return string
    */
	public function captchaInput($name, $attribs = array())
	{
		// TODO
		
		
		/*
		$this->addId($attribs, $name);
        $this->addClass($attribs, 'image');

		$xhtml = '<>';

        $xhtml = '<input type="text"'
                . ' name="' . $this->helper->escape($name) . '"'
                . $this->htmlAttribs($attribs)
                . $this->getEndTag();

        return $xhtml;
		 */
	}


}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_captcha.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_captcha.php']);
}
